package com.quanyue.haircut.shop.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.quanyue.haircut.domain.constant.UserRole;
import com.quanyue.haircut.domain.entity.IntegralConvert;
import com.quanyue.haircut.domain.entity.UserIntegral;
import com.quanyue.haircut.shop.dto.IntegralOrderDetail;
import com.quanyue.haircut.shop.service.IntegralService;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@Api(tags = "积分")
@RestController
@RequestMapping("/api")
public class IntegralController {
    @Autowired
    private IntegralService integralService;

    @ApiOperation(value = "更新积分")
    @PutMapping(value = "/integral", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <UserIntegral> updateIntegral(@RequestBody UserIntegral userIntegral) {
        return ServiceResult.success(integralService.updateIntegral(userIntegral.getUserId(),
                userIntegral.getShopId(),
                userIntegral.getIntegral()));
    }

    @ApiOperation(value = "积分商品消费记录")
    @PostMapping(value = "/integral/product/record", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> integralProductRecord(@RequestBody IntegralConvert integralConvert) {
        integralService.integralProductRecord(integralConvert);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "我的积分")
    @Scope(role = UserRole.USER)
    @GetMapping(value = "/integral", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <UserIntegral> myIntegral(@RequestParam Integer shopId,
                                                   @RequestParam Integer userId) {
        return ServiceResult.success(integralService.findIntegral(userId, shopId));
    }

    @ApiOperation(value = "积分订单")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/integral", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Page <IntegralOrderDetail>> integralOrder(@RequestBody PageRequest request,
                                                                    @RequestParam Integer shopId) {
        return ServiceResult.success(integralService.findIntegralOrder(shopId, request));
    }
}
